package com.songcha.module_main.ui.fragment.rank

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.songcha.library_base.base.BaseFragment
import com.songcha.library_business.config.VarConfig
import com.songcha.library_business.helper.AdvertCoverHelper
import com.songcha.library_business.helper.AdvertHelper
import com.songcha.library_common.ui.view.BottomNavigationView
import com.songcha.library_common.ui.view.CustomContainerLayout
import com.songcha.library_common.ui.view.NoScrollViewPager
import com.songcha.module_main.R
import com.songcha.module_main.event.RefreshGongDeEvent
import com.songcha.module_main.ui.adapter.RankTabAdapter
import org.greenrobot.eventbus.EventBus

class RankFragment:BaseFragment(),BottomNavigationView.IOnBottomNavigationItemChangeListener {
    private var mIsCurrentBottomTab = false

    init {
        TAG = RankFragment::class.java.simpleName
    }

    private var mIsLoadAdvert = false

    override fun getLayoutId(): Int {
        return R.layout.main_frag_rank
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        val cclGdRank = mRootView!!.findViewById<CustomContainerLayout>(R.id.ccl_gd_rank)
        val cclAchievement = mRootView!!.findViewById<CustomContainerLayout>(R.id.ccl_achievement)
        val tvGdRank = mRootView!!.findViewById<TextView>(R.id.tv_gd_rank)
        val tvAchievement = mRootView!!.findViewById<TextView>(R.id.tv_achievement)
        val vp = mRootView!!.findViewById<NoScrollViewPager>(R.id.main_nsvp)

        vp.adapter = RankTabAdapter(childFragmentManager)
        vp.offscreenPageLimit = 2

        cclGdRank.setOnClickListener {
            cclGdRank.setContainerBackgroundColor(Color.parseColor("#A86436"))
            tvGdRank.setTextColor(Color.parseColor("#FFDDC6"))
            cclAchievement.setContainerBackgroundColor(Color.TRANSPARENT)
            tvAchievement.setTextColor(Color.parseColor("#AB8B78"))
            vp.currentItem = 0
        }

        cclAchievement.setOnClickListener {
            cclAchievement.setContainerBackgroundColor(Color.parseColor("#A86436"))
            tvAchievement.setTextColor(Color.parseColor("#FFDDC6"))
            cclGdRank.setContainerBackgroundColor(Color.TRANSPARENT)
            tvGdRank.setTextColor(Color.parseColor("#AB8B78"))
            vp.currentItem = 1

            initAdvert()
        }

    }

    private fun initAdvert(){
        if(mIsLoadAdvert) return
        AdvertCoverHelper.loadAdvertByCover(requireActivity(),
            AdvertHelper.ADVERT_TYPE_INSERT_SCREEN,
            VarConfig.ADVERT_ACHIEVEMENT_INSERT_SCREEN_INFO,
            null,0f,object: AdvertHelper.ILoadAdvertCommonListener{
                override fun onSuccess(type: Int, height: Int, view: View?) {
                    mIsLoadAdvert = true
                }

                override fun onError(type: Int, code: Int, msg: String) {

                }

                override fun onClose(type: Int) {

                }

            })
    }

    fun isCurrentBottomTab():Boolean{
        return mIsCurrentBottomTab
    }

    override fun onBottomNavigationItemChange(position: Int, tag: String) {
        if(tag == TAG){
            mIsCurrentBottomTab = true
            EventBus.getDefault().post(RefreshGongDeEvent())
        }else{
            mIsCurrentBottomTab = false
        }
    }

}